//
// @author				mitchell
// @description		020101.html页面的JS打包文件 注意引用顺序
// @lastmodified		$2010-7 - 15$
$(document).ready(function(){
	function print(){
		var HKEY_Root,HKEY_Path,HKEY_Key; 
		HKEY_Root="HKEY_CURRENT_USER"; 
		HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; 
		var head,foot,top,bottom,left,right;

		//取得页面打印设置的原参数数据
		function PageSetup_temp() {
			try 
		 { 
		  var Wsh=new ActiveXObject("WScript.Shell"); 
		  HKEY_Key="header"; 
		//取得页眉默认值
		  head = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		  HKEY_Key="footer"; 
		//取得页脚默认值
		  foot = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		  HKEY_Key="margin_bottom"; 
		//取得下页边距
		  bottom = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		  HKEY_Key="margin_left"; 
		//取得左页边距
		  left = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		  HKEY_Key="margin_right"; 
		//取得右页边距
		  right = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		  HKEY_Key="margin_top"; 
		//取得上页边距
		  top = Wsh.RegRead(HKEY_Root+HKEY_Path+HKEY_Key); 
		 } 
		 catch(e){
		    alert("不允许ActiveX控件");
		 } 
		}

		//设置网页打印的页眉页脚和页边距
		function PageSetup_Null() 
		{ 
		 try 
		 { 
		  var Wsh=new ActiveXObject("WScript.Shell"); 
		  HKEY_Key="header"; 
		//设置页眉（为空）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 
		  HKEY_Key="footer"; 
		//设置页脚（为空）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
		  HKEY_Key="margin_bottom"; 
		//设置下页边距（0）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
		  HKEY_Key="margin_left"; 
		//设置左页边距（0）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
		  HKEY_Key="margin_right"; 
		//设置右页边距（0）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
		  HKEY_Key="margin_top"; 
		//设置上页边距（8）
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0"); 
		 
		 } 
		 catch(e){
		    alert("不允许ActiveX控件");
		 } 
		} 
		//设置网页打印的页眉页脚和页边距为默认值 
		function  PageSetup_Default() 
		{   
		 try 
		 { 
		  var Wsh=new ActiveXObject("WScript.Shell"); 
		  HKEY_Key="header"; 
		  HKEY_Key="header"; 
		//还原页眉
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,head); 
		  HKEY_Key="footer"; 
		//还原页脚
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,foot); 
		  HKEY_Key="margin_bottom"; 
		//还原下页边距
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,bottom); 
		  HKEY_Key="margin_left"; 
		//还原左页边距
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,left); 
		  HKEY_Key="margin_right"; 
		//还原右页边距
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,right); 
		  HKEY_Key="margin_top"; 
		//还原上页边距
		  Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,top); 
		  
		 }
		 catch(e){
		 	alert("不允许ActiveX控件");
		 }
		}

		function getObj(id)

		{

		    return document.getElementById(id);

		}
		/*
		function printorder(ContentId)
		{		
			var s = ifrm.document.getElementById("printorder");
			s.innerHTML = document.getElementById(ContentId).innerHTML;
			PageSetup_temp();//取得默认值
			PageSetup_Null();//设置页面
			document.all.ifrm.ExecWB(6,2);//打印页面
			PageSetup_Default();//还原页面设置
		}
		*/
		PageSetup_temp();//取得默认值
		PageSetup_Null();//设置页面
		//document.all.ifrm.ExecWB(6,2);//打印页面
		PageSetup_Default();//还原页面设置
	}

	//打印
	$(".print").click(function(event){
		event.preventDefault();
		
		var _this = this;
		var type = $(_this).attr('type');
		
		var oid = $(_this).attr('oid');
		var sid = $(_this).attr('sid');
		var tel = $(".tel_"+oid).html();
		var address = $(".address_"+oid).html();
		var username = $(".username_"+oid).html();
		var ordertime = $(".ordertime_"+oid).html();
		var price = $(".price_"+oid).html();
		if(!tel){ alert("收货人电话不存在，不能打印"); return false;}
		if(!username){ alert("收货人姓名不存在，不能打印"); return false;}
		if(!address){ alert("收货人地址不存在，不能打印"); return false;}
		$(".printorder_"+oid).show();
		var url = js_context.base_url+"ajax/user/printorder";
		$.get(url,{"type" : type,"oid" : oid , 'sid' : sid , 'tel' : tel ,'address' : address , 'username' : username , 'ordertime' : ordertime, 'price' : price},function(result){
			$(".printorder_"+oid).hide();
			if(result.success == false){
				alert('通讯失败');
				return false;
			}
			var data = result.message;
			if(result.leve == 5){
				print();
				var s = ifrm.document.getElementById("printorder");
				s.innerHTML = data;
				document.all.ifrm.ExecWB(6,2);//打印页面
			    
			}
			if(!data){
				alert('打印失败');
				return false;
			}
			$(_this).hide();
			$(".print_old_"+oid).show();
			$(".print_fill_"+oid).show();
		},"json");
	});
	//
	$(".order_div").click(function(event){
		event.preventDefault();
		var div = $(this).attr("type");
		$(".order_div").show();
		$("."+div).hide();
		$(".or-hunt1").slideToggle(800);
	});
	$(".order_status").change(function(){
		var status = $(this).children('option:selected').val();
		var url = $(this).attr("url");
		self.location = url+"&status="+status+"#position";
		return false;
	});
	
	//storefood管理
	$(".orderfood").click(function(event){
		event.preventDefault();
		$("#my-list-total").show();
		$("#mask").show();
		//悬浮框
		var st = $(document).scrollTop();
		$("#my-list-total").css("top", st+138);
		
		var _this = this;
		var oid = $(_this).attr("oid");
		var sid = $(_this).attr("sid");
		var url = $(_this).attr("url");
		$(".order_"+oid).show();
		$(".ordercontent_list").hide();
		if(oid == $(".ordercontent_"+oid).attr("oid")){
			$(".ordercontent_"+oid).show();
			$(".order_"+oid).hide();
			return false;
		}
		
		$.get(url,{"oid" : oid , 'sid' : sid},function(result){
			$(".order_"+oid).hide();
			if(result.success == false){
				alert('通讯失败');
				return false;
			}
			var data = result.message;
			
			if(!data){
				alert('查询失败');
				return false;
			}
			
			var html = '';
			var priceall = 0;
			var price = 0;
			var price1 = 0;
			var price2 = 0;
			html += '<div class="ordercontent_list ordercontent_'+oid+'" oid="'+oid+'">';
			for(var i=0;i<data.length;i++){
				var g_sid = data[i].sid;
				html += '<h1><a href="'+js_context.base_url+'store/'+data[i].sid+'">'+data[i].storename+'</a></h1>';
				html += '<h2><span><strong>订单状态：</strong><font>';
				if(data[i].status == 1){
					html += '未配送';
				}
				if(data[i].status == 0){
					html += '已配送';
				}
				html += '</font>';
				html += '</span>';
				html += '<span><strong>送餐费用：</strong><font>';
				if(data[i].servecost==0 || $.trim(data[i].servecost)==null || $.trim(data[i].servecost)=="免费"){
					html += '免运费';
					 price1 += 0;
				}else{
					html += +parseInt(data[i].servecost)+'元';
					price2 += parseInt(data[i].servecost);
				}
				html += '</font></span>';
				html += '</h2>';
				
				html += '<div id="commodity-list">';
				html += '<div id="co-list">';
				html += '<div id="co-list-name">商品名称</div>';
				html += '<div id="co-list-price">价格</div>';
				html += '<div id="co-list-price">数量</div>';
				html += '</div>';
				for(var y=0;y<data[i].food.length;y++){
					html += '<div class="co-list">';
					html += '<div class="co-list-name"><a title="'+data[i].food[y].foodname+'" href="'+js_context.base_url+'food/'+data[i].food[y].fid+'">'+data[i].food[y].foodname.substr(0,15)+'</a></div>';
					html += '<div class="co-list-price">'+data[i].food[y].price+'</div>';
					html += '<div class="co-list-num">'+data[i].food[y].count+'</div>';
					html += '</div>';
					priceall += data[i].food[y].price*data[i].food[y].count;
				}
				html += '</div>';
			}
			
				html += '<div class="co-list-total">';
				html += '<font>元</font><span>'+(price1+price2+priceall)+'</span><font>总计：</font>';
				html += '</div>'; 
				html += '</div>'; 
			$("#order_list").append(html);
			
		},"json");
	});
	
	$(".close_pic").click(function(event){
		$("#my-list-total,#mask").hide();
	});
	
	$(document).keyup(function(e){
		if(e.which == 27){
			var temp= $("#my-list-total").is(":visible");//是否可见
			if(!temp){
				return false;
			}
			$("#my-list-total,#mask").hide();
		}
	});
	
	//user order
	$(".orderlist").click(function(event){
		event.preventDefault();
		$("#my-list-total").show();
		$("#mask").show();
		//悬浮框
		var st = $(document).scrollTop();
		$("#my-list-total").css("top", st+138);
		
		var _this = this;
		var oid = $(_this).attr("oid");
		var url = $(_this).attr("url");
		$(".order_"+oid).show();
		$(".ordercontent_list").hide();
		if(oid == $(".ordercontent_"+oid).attr("oid")){
			$(".ordercontent_"+oid).show();
			$(".order_"+oid).hide();
			return false;
		}
		
		$.get(url,{"oid" : oid},function(result){
			$(".order_"+oid).hide();
			if(result.success == false){
				alert('通讯失败');
				return false;
			}
			var data = result.message;
			
			if(!data){
				alert('查询失败');
				return false;
			}
			
			var html = '';
			var priceall = 0;
			var price = 0;
			var price1 = 0;
			var price2 = 0;
			html += '<div class="ordercontent_list ordercontent_'+oid+'" oid="'+oid+'">';
			for(var i=0;i<data.length;i++){
				var g_sid = data[i].sid;
				html += '<h1><a href="'+js_context.base_url+'store/'+data[i].sid+'">'+data[i].storename+'</a></h1>';
				html += '<h2><span><strong>电话：</strong><font>';
				/*关闭400电话显示
				if(data[i].tel400 != ''){
					html += '400-010-1114 (转)<label> '+data[i].tel400+'</label>';
				}else{
					html += data[i].tel;
				}
				*/
				html += data[i].tel;
				html += '</font>';
				html += '</span>';
				//html +='<a class="report_'+data[i].sid+'" href="javascript:void(0)">纠错</a>';
				html += '<span><strong>送餐费用：</strong><font>';
				if(data[i].servecost==0 || $.trim(data[i].servecost)==null || $.trim(data[i].servecost)=="免费"){
					html += '免运费';
					 price1 += 0;
				}else{
					html += +parseInt(data[i].servecost)+'元';
					price2 += parseInt(data[i].servecost);
				}
				html += '</font></span>';
				html += '</h2>';

				html += '<div class="error-recovery edit_tel hide">';
				html +=	'<strong><input type="text" class="r_tel_'+data[i].sid+'" sid="'+data[i].sid+'" value=""/></strong>';
				html +=	'<input type="button" value="" class="inputjiu_'+data[i].sid+'" />';
				html += '</div>';
				
				html += '<div id="commodity-list">';
				html += '<div id="co-list">';
				html += '<div id="co-list-name">商品名称</div>';
				html += '<div id="co-list-price">价格</div>';
				html += '<div id="co-list-price">数量</div>';
				html += '</div>';
				for(var y=0;y<data[i].food.length;y++){
					html += '<div class="co-list">';
					html += '<div class="co-list-name">';
					if(data[i].food[y].foodname != ''){
						html += '<a title="'+data[i].food[y].foodname+'" href="'+js_context.base_url+'food/'+data[i].food[y].fid+'">'+data[i].food[y].foodname.substr(0,15)+'</a>';
					}else{
						html += '餐品已下架';
					}
					html +='</div>';
					html += '<div class="co-list-price">'+data[i].food[y].price+'</div>';
					html += '<div class="co-list-num">'+data[i].food[y].count+'</div>';
					html += '</div>';
					priceall += data[i].food[y].price*data[i].food[y].count;
				}
				html += '</div>';
				if(data[i].level == 0){
					html += '<div class="Just-Call">';
					html += '请打电话订餐，打电话时，您可以这样说：“您好，我在<a href="'+js_context.base_url+'">外卖网</a>上看见...”';
					html += '</div>';
				}
			}
			
				html += '<div class="co-list-total">';
				html += '<font>元</font><span>'+(price1+price2+priceall)+'</span><font>总计：</font>';
				html += '</div>'; 
				html += '</div>'; 
			$("#order_list").append(html);
			
			//电话纠错
			$(".report_"+g_sid).click(function(event){
				event.preventDefault();
				$(".edit_tel").show();
			});
			
			$(".inputjiu_"+g_sid).click(function(event){
				event.preventDefault();
				var tel = $(".r_tel_"+g_sid).val();
				var sid = $(".r_tel_"+g_sid).attr("sid");
				var url = js_context.base_url+"ajax/store/report_tel";
				
				if(tel==''||tel=='请输入电话'){
					alert("请输入电话");
					$('.r_tel').focus();
					return false; 
				}
				var myreg = /^[-\d]+$/;
				if(!myreg.test(tel)){
					alert("您输入的电话有误！");
					return false; 
				}
				$.get(url, {'tel' : tel,'sid' : sid}, function(result){
					if(result.success == false){
						alert('通讯失败');
						return false;
					}
					var data = result.message;
					if(!data){
						alert('操作失败');
						return false;
					}
					if(data['error'] == 'notlogin'){
						alert('请先登录');
						$(".edit_tel").hide();
						return false;
					}
					if(data['error'] == 'timeout'){
						alert('请您半小时后再提交！');
						$(".edit_tel").hide();
						return false;
					}
					alert("提交成功！谢谢您的参与");
					$(".edit_tel").hide();
				},"json")
			});
			
		},"json");
	});
	
	$(".order_load").click(function(event){
		event.preventDefault();
		var page = $(".page").val();
		var url = js_context.base_url+"ajax/user/get_order";
		$.get(url,{"page" : (Number(page)+1)},function(result){
			if(result.success == false){
				alert('通讯失败');
				return false;
			}
			var data = result.message;
			if(!data){
				$(".order_load").hide();
				return false;
			}
			if(result.login == false){
				alert('请先登录');
				return false;
			}
			var html = '';
			for(var i=0;i<data.length;i++){
				html += '<div class="my-order'
				if(data[i].level==0){
					html += '2">';		
				}else{
					html += '1">';
				}
				html += '<div class="my-order1-num">'+data[i].oid+'</div>';
				html += '<div class="my-order1-user">';
				if(data[i].a_name){
					html += data[i].a_name;
				}else{
					html += data[i].username;
				}
				html += '</div>';
				html += '<div class="my-order1-user">货到付款</div>';
				
				html += '<div class="my-order1-date">';
				html += '<span>'+data[i].created+'</span>';
				html += '<span>货到付款</span>';
				html += '</div>';
				html += '<div class="my-order1-date"><span>';
				if(data[i].status==0){
					html += '已关闭';	
				}else{
					html += '下单中';//下单中
				}	
				html += '</span><span>';
				if(data[i].level==0){
					html += '请自行打电话订餐';	
				}else{
					html += '商家自动配送';//下单中
				}
				html += '</span></div>';
				
				html += '<div class="my-order1-handle">';
				html +=	'<span>';
				html += '<a href="javascript:void(0)" class="orderlist_'+page+'" url="'+js_context.base_url+'ajax/user/orderContent" oid="'+data[i].oid+'">查看订单</a>';
				html += '<img class="order_loading hide order_'+data[i].oid+'" src="'+js_context.base_url+'res/images/loading.gif"/>';
				html += '</span>';                       
				html += '<p><input type="button2" value="" onclick="location.href=\''+js_context.base_url+'\'"/></p>';
				html += '</div>';
				html +=	'</div>';
				
			}
			$("#orderList").append(html);
			$(".page").val(Number(page)+1);
			if(data.length<10){
				$(".uploading").hide();
				return false;
			}

			$(".orderlist_"+page).click(function(event){
				event.preventDefault();

				$("#my-list-total").show();
				$("#mask").show();
				
				//悬浮框
				var st = $(document).scrollTop();
				$("#my-list-total").css("top", st+138);
				
				var _this = this;
				var oid = $(_this).attr("oid");
				var url = $(_this).attr("url");
				$(".order_"+oid).show();
				$(".ordercontent_list").hide();
				if(oid == $(".ordercontent_"+oid).attr("oid")){
					$(".ordercontent_"+oid).show();
					$(".order_"+oid).hide();
					return false;
				}
				
				$.get(url,{"oid" : oid},function(result){
					$(".order_"+oid).hide();
					if(result.success == false){
						alert('通讯失败');
						return false;
					}
					var data = result.message;
					
					if(!data){
						alert('查询失败');
						return false;
					}
					
					var html = '';
					var priceall = 0;
					var price = 0;
					var price1 = 0;
					var price2 = 0;
					html += '<div class="ordercontent_list ordercontent_'+oid+'" oid="'+oid+'">';
					for(var i=0;i<data.length;i++){
						
						var g_sid = data[i].sid;
						html += '<h1><a href="'+js_context.base_url+'store/'+data[i].sid+'">'+data[i].storename+'</a></h1>';
						html += '<h2><span><strong>电话：</strong><font>'+data[i].tel+'</font>';
						html += '</span>';
						//html +='<a class="report_'+data[i].sid+'" href="javascript:void(0)">纠错</a>';
						html += '<span><strong>送餐费用：</strong><font>';
						

						if(data[i].servecost==0 || $.trim(data[i].servecost)==null || $.trim(data[i].servecost)=="免费"){
							html += '免运费';
							 price1 += 0;
						}else{
							html += +parseInt(data[i].servecost)+'元';
							price2 += parseInt(data[i].servecost);
						}
						html += '</font></span>';
						html += '</h2>';
						

						html += '<div class="error-recovery edit_tel hide">';
						html +=	'<strong><input type="text" class="r_tel_'+data[i].sid+'" sid="'+data[i].sid+'" value=""/></strong>';
						html +=	'<input type="button" value="" class="inputjiu_'+data[i].sid+'" />';
						html += '</div>';
						
						html += '<div id="commodity-list">';
						html += '<div id="co-list">';
						html += '<div id="co-list-name">商品名称</div>';
						html += '<div id="co-list-price">价格</div>';
						html += '<div id="co-list-price">数量</div>';
						html += '</div>';
						for(var y=0;y<data[i].food.length;y++){
							html += '<div class="co-list">';
							html += '<div class="co-list-name"><a href="'+js_context.base_url+'food/'+data[i].fid+'">'+data[i].food[y].foodname+'</a></div>';
							html += '<div class="co-list-price">'+data[i].food[y].price+'</div>';
							html += '<div class="co-list-num">'+data[i].food[y].count+'</div>';
							html += '</div>';
							priceall += data[i].food[y].price*data[i].food[y].count;
						}
						html += '</div>';
						if(data[i].level == 0){
							html += '<div class="Just-Call">';
							html += '请打电话订餐，打电话时，您可以这样说：“您好，我在<a href="'+js_context.base_url+'">外卖网</a>上看见...”';
							html += '</div>';
						}
					}
						html += '<div class="co-list-total">';
						html += '<font>元</font><span>'+(price1+price2+priceall)+'</span><font>总计：</font>';
						html += '</div>'; 
						html += '</div>'; 
					$("#order_list").append(html);
					
					
					//电话纠错
					$(".report_"+g_sid).click(function(event){
						event.preventDefault();
						$(".edit_tel").show();
					});
					
					$(".inputjiu_"+g_sid).click(function(event){
						event.preventDefault();
						var tel = $(".r_tel_"+g_sid).val();
						var sid = $(".r_tel_"+g_sid).attr("sid");
						var url = js_context.base_url+"ajax/store/report_tel";
						
						if(tel==''||tel=='请输入电话'){
							alert("请输入电话");
							$('.r_tel').focus();
							return false; 
						}
						var myreg = /^[-\d]+$/;
						if(!myreg.test(tel)){
							alert("您输入的电话有误！");
							return false; 
						}
						$.get(url, {'tel' : tel,'sid' : sid}, function(result){
							if(result.success == false){
								alert('通讯失败');
								return false;
							}
							var data = result.message;
							if(!data){
								alert('操作失败');
								return false;
							}
							if(data['error'] == 'notlogin'){
								alert('请先登录');
								$(".edit_tel").hide();
								return false;
							}
							if(data['error'] == 'timeout'){
								alert('请您半小时后再提交！');
								$(".edit_tel").hide();
								return false;
							}
							alert("提交成功！谢谢您的参与");
							$(".edit_tel").hide();
						},"json")
					});
					
					
				},"json");
			});
			
			
		},"json");
	});
	
	var unread = js_context.last_oid;
	var checkUnread = function() {
		var interval = setInterval(function(){
			var sid = js_context.sid;
			
			checkData(unread,sid);
		}, 20000);
	};
	$(".ts_gb").click(function(event){
		event.preventDefault();
		$(".ts_tc,#mask").hide(); 
	});
	$(document).keyup(function(e){
		if(e.which == 27){
			var temp= $(".ts_tc,#mask").is(":visible");//是否可见
			if(!temp){
				return false;
			}
			$(".ts_tc,#mask").hide();
		}
	});
	function checkData(last_oid,sid){
		$.get(js_context.base_url+"ajax/search/get_order",{'oid':last_oid,'sid':sid}, function(result){
			if (!result.success || result.unread == '')
				return;
			var order = result.unread.order;

			if (result.unread.since_id !== undefined) {
				unread = result.unread.since_id;
				for(var i=0;i<order.length;i++){
					
				}

				//有新订单;
				var st = $(document).scrollTop();
				$(".ts_tc").css("margin-top",st).show();
				$("#mask").show();
				if(result.level == 5){
					print();
					var data = result.html;
					var s = ifrm.document.getElementById("printorder");
					s.innerHTML = data;
					document.all.ifrm.ExecWB(6,2);//打印页面
				}
				location.reload();
				return;
				//show message
				//TODO
			}
		},"json");
	}
	
	checkUnread();

});
